//
//  AppDelegate.h
//  Address-book📞
//
//  Created by Mars_ZXB on 16/8/6.
//  Copyright © 2016年 BAT3. All rights reserved.
//


#define FRAME_WIDTH self.contentView.frame.size.width
#define FRAME_HEIGHT self.contentView.frame.size.height

#import "CellOfContact.h"
#import "ModelOfContact.h"

@interface CellOfContact ()

@property (nonatomic, retain) UIImageView *picOfPerson;

@property (nonatomic, retain) UILabel *labelOfName;

@property (nonatomic, retain) UILabel *labelOfPhoneNumber;

@end

@implementation CellOfContact

#pragma mark - 重写初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}

/** 创建子视图 */
- (void)createSubViews {
    self.picOfPerson = [[UIImageView alloc]init];
    [self.contentView addSubview:self.picOfPerson];
    self.labelOfName = [[UILabel alloc]init];
    [self.contentView addSubview:self.labelOfName];
    
    self.labelOfPhoneNumber = [[UILabel alloc]init];
    [self.contentView addSubview:self.labelOfPhoneNumber];
}

#pragma mark - 重写子视图布局.
- (void)layoutSubviews {
    
    [super layoutSubviews];
    self.picOfPerson.frame = CGRectMake( 5, 10,  FRAME_WIDTH/ 4.f, FRAME_HEIGHT - 20);
    self.picOfPerson.backgroundColor = [UIColor yellowColor];
    //产生一个随机数
    int nameImage = arc4random()%41;
    NSString *str = [NSString stringWithFormat:@"%02d", nameImage];
    NSString *path = [[NSBundle mainBundle] pathForResource:str ofType:@"jpg"];
    self.picOfPerson.image = [UIImage imageNamed:path];
    self.labelOfName.frame = CGRectMake(FRAME_WIDTH / 3.3, 10, FRAME_WIDTH / 3.f, FRAME_HEIGHT / 3.5);
    
    self.labelOfPhoneNumber.frame = CGRectMake(FRAME_WIDTH / 3.3, 20 + FRAME_HEIGHT / 3.5, FRAME_WIDTH / 2.5, FRAME_HEIGHT / 3.5);
 
}

#pragma mark - 赋值.
- (void)setContentValueWithModel:(ModelOfContact *)model {
    
    self.modelOfCell = model;
    
    self.picOfPerson.image = model.image;
    
    self.labelOfName.text = model.name.capitalizedString;
    
    self.labelOfPhoneNumber.text = model.phoneNumber;



}

#pragma mark - 触摸结束, 点击事件.
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.target performSelector:self.action withObject:self afterDelay:0];
}

- (void)addTarget:(id)target action:(SEL)action {
    self.target = target;
    self.action = action;
}



#pragma mark - other
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
